package persistencia;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import negocio.Clase;
import negocio.Cursada;
import negocio.Sala;

public class ClaseDAO 
{
	private Connection c;
	private static ClaseDAO instancia;
	
	private ClaseDAO()
	{
		c = BDManager.getInstance().getConexion();
	}
	
	public static ClaseDAO  getInstance()
	{
		if(instancia == null)
			instancia = new ClaseDAO();
		return instancia;
	}

	public ArrayList<Clase> getClases(Cursada cursada)
	{
		String query = "SELECT * FROM clase WHERE cursada_fk =" + cursada.getId();
		Statement s;
		ResultSet rs;
		ArrayList<Clase> res = new ArrayList<Clase>();
		try {
			s = c.createStatement();
			rs = s.executeQuery(query);
			while(rs.next())
			{
				Sala sala = SalaDAO.getInstance().getSala(rs.getInt(7));
				res.add(new Clase(rs.getInt(1), cursada, rs.getDate(2), rs.getTime(3), rs.getTime(4), rs.getString(5), sala));
			}
			
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return res;
	}
	
	public void guardar(Clase clase)
	{
		String query = "INSERT INTO clase(fecha, horainicio, horafin, estado, cursada_fk, sala_fk) VALUES ('" + 
				clase.getFecha().toString() + "','" + clase.getHoraInicio().toString() + "','"+ clase.getHoraFin().toString()+"','"+
				clase.getEstado().toString() + "'," + clase.getCursada().getId()+","+clase.getSala().getId()+")";
		try {
			Statement s =c.createStatement();
			s.execute(query);
			s.close();
		} catch (SQLException e) {
			e.printStackTrace();		
		}
		
	}
}
